module.exports = {
  async getPermissions(ctx) {
    const menus = []
    const points = []

    const getPermissions = async role => {
      const roles = await strapi.query('Roles').find({ id: role.id })
      roles[0].permissions.forEach(el => {
        Number(el.type) === 1 ? menus.push(el.code) : points.push(el.code)
      })
    }

    const fixDuplicate = (arr) => {
      const set = new Set()
      arr.forEach(element => {
        set.add(element)
      });
      return Array.from(set)
    }

    const { id } = ctx.state.user
    const { roles } = await strapi.db.query('plugins::users-permissions.user').findOne({
      id
    })

    for (let index = 0; index < roles.length; index++) {
      const role = roles[index];
      await getPermissions(role)
    }

    return {
      menus: fixDuplicate(menus),
      points: fixDuplicate(points)
    }
  }
}
